package com.wandian.cqcsk.mvvm.mine.invite;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.config.CoinTypeEnum;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.tools.ShareTools;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.QRCodeUtil;
import com.mylib.libcore.utils.StatusBarUtil;
import com.mylib.libcore.utils.StatusBarUtils;
import com.mylib.libcore.utils.StringUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.ViewUtils;
import com.wandian.cqcsk.R;
import com.wandian.cqcsk.databinding.ActivityInviteLayoutBinding;

import static com.mylib.libcore.config.AppSPKeyConfig.APP_DOWNLOAD_URL;
import static com.mylib.libcore.config.AppSPKeyConfig.INVITE_CODE_RUL;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description InviteActivity邀请
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_INVITE)
public class InviteActivity extends BaseMVVMActivity<InviteViewModel> {
    private ActivityInviteLayoutBinding dataBinding;
    private Bitmap share_bitmap = null;
    private UserInfoBean userInfoBean;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_invite_layout);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        UIUtils.setKeyEveryDayCoin(CoinTypeEnum.INVITE_COIN);
        userInfoBean =AppContext.getInstance().getAppPreferencesHelper().getUserInfoBean();
        initViewData();
        initClink();
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.bindSysToolBar.viewSystem,true);
        dataBinding.bindSysToolBar.toolBarIvBack.setVisibility(View.VISIBLE);
        dataBinding.bindSysToolBar.toolBarTvTitle.setVisibility(View.VISIBLE);
        dataBinding.bindSysToolBar.toolBarTvTitle.setText("邀请好友");
        dataBinding.tvInviteId.setText(userInfoBean.getInvitationCode()+"");
        String invite_url = AppContext.getInstance().getAppPreferenceForever().getKeyValue(INVITE_CODE_RUL)+"";
        if(!UIUtils.isEmpty(invite_url)){
             Bitmap logoBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.img_ic_launcher);
            final Bitmap er_bitmap = QRCodeUtil.createQRImage(invite_url, 110, 110, logoBitmap, null);
            if(!UIUtils.isEmpty(er_bitmap)){
                dataBinding.ivShareEr.setImageBitmap(er_bitmap);
            }
        }
         share_bitmap = ViewUtils.toBitmap(dataBinding.rlShareCenter);
    }

    private void initClink(){
        dataBinding.bindSysToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        dataBinding.llShareWx.setOnClickListener(v -> sharePic("微信"));
        dataBinding.llSharePyq.setOnClickListener(v -> sharePic("朋友圈"));
        dataBinding.llShareQq.setOnClickListener(v -> sharePic("QQ"));
        dataBinding.llShareCopy.setOnClickListener(v -> StringUtil.copyToClip(this, AppContext.getInstance().getAppPreferenceForever().getKeyValue(APP_DOWNLOAD_URL)+""));
    }

    private void sharePic(String text){
        if(UIUtils.isEmpty(share_bitmap)){
            share_bitmap = ViewUtils.toBitmap(dataBinding.rlShareCenter);
        }
        if(!UIUtils.isEmpty(share_bitmap)){
            switch (text){
                case "微信":
                    ShareTools.shareWxPic(getContext(),share_bitmap,0);// 0：分享到好友  1：分享到朋友圈
                    break;
                case "朋友圈":
                    ShareTools.shareWxPic(getContext(),share_bitmap,1);// 0：分享到好友  1：分享到朋友圈
                    break;
                case "QQ":
                    ToastUtil.show("分享QQ");
                    break;
                case "复制":
                    ToastUtil.show("已复制");
                    break;
            }

        }
    }


}
